﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SundStudent.ControlLayer;
using SundStudent.ModelLayer;
using System.Windows.Forms.DataVisualization.Charting;

namespace SundStudent.GUILayer
{
    public partial class TemperaturForm : Form
    {
        PatientControler ctrlPatient = new PatientControler();
        private LektionsPatient selectedPatient;
        private string[] tidsarray = { "06:00", "12:00", "18:00", "24:00" };

        public TemperaturForm(LektionsPatient patient)
        {
            InitializeComponent();
            selectedPatient = patient;
            labelAktuelPatient.Text = selectedPatient.Navn +  " Stue: " + selectedPatient.Stue.Stuenr + " " +selectedPatient.Seng.Sengnr;
            // Set palette.
            this.chart1.Palette = ChartColorPalette.SeaGreen;

            // Set title.
            this.chart1.Titles.Add("Temperatur");
            this.chart2.Palette = ChartColorPalette.Pastel;

            // Set title.
            this.chart2.Titles.Add("Puls");
            opdaterListe();


        }



        private void button1_Click(object sender, EventArgs e)
        {
            String dato = dateTimePicker1.Text.ToString();
            DateTime dato1 = new DateTime();
            dato1 = DateTime.Parse(dato);


            Console.WriteLine("Dato " + dato);
            int index = comboBox1.SelectedIndex;
            string tid = tidsarray[index];
            string blodtryk = "";
            try
            {

                int blod1 = checkBlodtryk(textBoxBlod1.Text.ToString());
                int blod2 = checkBlodtryk(textBoxBlod2.Text.ToString());
                blodtryk = blod1 + "/" + blod2;

                double temperatur = checkTemperatur(textBoxTemperatur.Text.ToString());
                int puls = Convert.ToInt32(textBoxPuls.Text.ToString());
                string resp = "";
                resp = textBoxReps.Text.ToString();
                int intReso = Convert.ToInt32(resp);
                if (intReso < 3 || intReso > 40)
                {
                    throw new OpretException("Respiration skal være i intervallet 4-40");
                }

                int pnr = selectedPatient.Nr;
                string initialer = "tin";
                PatientControler ctrlPatient = new PatientControler();
                int rc = ctrlPatient.RegistrerTemperatur(pnr, dato1, tid, blodtryk, puls, temperatur, resp, initialer, selectedPatient.IndDato);
                if (rc == 1)
                {
                    textBoxBlod1.Text = "";
                    textBoxBlod2.Text = "";
                    textBoxPuls.Text = "";
                    textBoxTemperatur.Text = "";
                    textBoxReps.Text = "";
                    labelMeddelelse.Text = "";
                    opdaterListe();
                }
                else
                {
                    label7.Text = "Der er sket en fejl i registreringen";

                }
            }
            catch (OpretException eOpret)
            {
                label7.Text = eOpret.toString();
            }
            catch (Exception e1)
            {
                label7.Text = "Blodtryk, puls,temperatur og respiration skal være tal";

            }

        }

        private void textBoxReps_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string resp = textBoxReps.Text.ToString();
                int intReso = Convert.ToInt32(resp);
                if (intReso < 3 || intReso > 40)
                {
                    throw new OpretException("Respiration skal være i intervallet 4-40");
                }

            }

            catch (OpretException eb)
            {
                label7.Text = eb.toString();
            }

            catch (Exception e1)
            {
                label7.Text = "Blodtryk, puls og temperatur  skal være tal";


            }

        }


        private void loadChart(List<Temperatur> templist)
        {
            this.chart1.Series.Clear();
            this.chart2.Series.Clear();

            // Add series.
            Series series = this.chart1.Series.Add((templist.ElementAt(0).Date).ToString());
            foreach (Temperatur tempObj in templist)
            {
                // Add point.
                series.Points.Add(Convert.ToDouble(tempObj.Temp));

            }
            Series series2 = this.chart2.Series.Add((templist.ElementAt(0).Date).ToString());
            foreach (Temperatur tempObj in templist)
            {
                // Add point.

                series2.Points.Add(Convert.ToInt32(tempObj.Puls.ToString()));
            }

        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }
        private void opdaterListe()
        {
            List<Temperatur> tempList = new List<Temperatur>();
            tempList = ctrlPatient.HentPatientsTemp(selectedPatient.Nr);
            listBox1.Items.Clear();
            if (tempList != null)
            {
                foreach (Temperatur tempObj in tempList)
                {
                    listBox1.Items.Add(Convert.ToString(tempObj.Date).Substring(0, 10) +
                        " " + tempObj.Tid
                        + " " + tempObj.Temp.ToString()
                        + " " + tempObj.Blodtryk);


                }
                if (tempList.Count > 0)
                {
                    loadChart(tempList);
                }


            }
        }


        private int checkBlodtryk(string blod)
        {
            int blodTryk = 0;
            try
            {
                blodTryk = Convert.ToInt32(blod);
                if (blodTryk < 0 || blodTryk > 360)
                {
                    throw new OpretException("Blodtryk værdi mellem 0 og 360");
                }
            }
            catch (OpretException eb)
            {
                label7.Text = eb.toString();

            }
            catch (Exception ee)
            {
                label7.Text = "Blodtryk skal være et tal mellem 0 og 360";

            }


            return blodTryk;
        }
        private double checkTemperatur(string temp)
        {
            double tempM = 0;
            try
            {


                tempM = Convert.ToDouble(temp);
                Console.WriteLine("temperatur " + tempM);
                if (tempM < 36 || tempM > 42)
                {
                    throw new OpretException("Temperatur skal være mellem 36 og 42");
                }
            }
            catch (OpretException eb)
            {
                label7.Text = eb.toString();

            }
            catch (Exception ee)
            {
                label7.Text = "Temperatur skal være mellem 36 og 42";

            }
            return tempM;
        }

        private void textBoxBlod1_TextChanged(object sender, EventArgs e)
        {

            int blod1 = checkBlodtryk(textBoxBlod1.Text.ToString());


        }

        private void textBoxBlod2_TextChanged(object sender, EventArgs e)
        {
            int blod1 = checkBlodtryk(textBoxBlod2.Text.ToString());
        }

        private void textBoxTemperatur_TextChanged(object sender, EventArgs e)
        {
            double temperatur = checkTemperatur(textBoxTemperatur.Text.ToString());
        }

        private void textBoxPuls_TextChanged(object sender, EventArgs e)
        {
            int puls = checkPuls(textBoxPuls.Text.ToString());
        }

        private int checkPuls(string puls)
        {
            int tempPuls = 0;
            try
            {


                tempPuls = Convert.ToInt32(puls);

                if (tempPuls < 30 || tempPuls > 230)
                {
                    throw new OpretException("Puls skal være mellem 30 og 230");
                }
            }
            catch (OpretException eb)
            {
                label7.Text = eb.toString();

            }
            catch (Exception ee)
            {
                label7.Text = "Puls skal være mellem 30 og 230";

            }
            return tempPuls;
        }

    }
}
